package com.lb.lb_ad_demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import com.lb.ad.ADv;
import com.lb.ad.SPUtils;
import com.lb.ad.cb.AdInterFace;
import com.lb.ad.type.BannerAD;
import com.lb.ad.type.NativeAD;
import android.util.Log;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example);
        SPUtils.save(MainActivity.this,"AA","satus");

        findViewById(R.id.btn_showsp).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SpActivity.StartSpAct(MainActivity.this);
            }
        });

        findViewById(R.id.btn_showinnner).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                showInner();

            }
        });

        findViewById(R.id.btn_showvideo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showRewwardVideo();
             }
        });

        findViewById(R.id.btn_showfullvideo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showFullVideo();
            }
        });

        findViewById(R.id.btn_showdrawinner).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //不同位置信息流需要实例化不同的对象
                loadNewNativeAD1();
            }
        });

        findViewById(R.id.btn_showdrawvideo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadNewNativeAD2();
            }
        });

        findViewById(R.id.btn_banner).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showBanner();
            }
        });
    }



    BannerAD bannerAD ;
    public void showBanner(){
        if(bannerAD==null) {
            bannerAD = new BannerAD();
        }
         bannerAD.showAD(LBAD_Config.BANNER_ID,MainActivity.this,(ViewGroup) findViewById(R.id.ll_banner), LBAD_Config.getBannerHeight(MainActivity.this), new AdInterFace.drawADCallBack() {
             //banner展示回调
             @Override
             public void show() {
                 Log.e("LBAD_BANNER","show");
             }
             //banner用户关闭和错误回调
             @Override
             public void userClose(String code, String msg, boolean isErr) {
                 Log.e("LBAD_BANNER","userClose"+isErr);

             }
             //banner用户点击
             @Override
             public void click() {
                 Log.e("LBAD_BANNER","click");

             }
         });
    }


    NativeAD nativeAD1;
    public NativeAD getNative1(){
        if(nativeAD1 == null) {
            nativeAD1 = new NativeAD(LBAD_Config.NATIVE_ID, MainActivity.this);
        }
        return nativeAD1;
    }

    NativeAD nativeAD2;
    public NativeAD getNative2(){
        if(nativeAD2 == null) {
            nativeAD2 = new NativeAD(LBAD_Config.NATIVE_ID2, MainActivity.this);
        }
        return nativeAD2;
    }


    //
    public void loadNewNativeAD1(){
        getNative1().load(LBAD_Config.getNativeAD1Width(MainActivity.this), LBAD_Config.getNativeAD1Height(MainActivity.this), new AdInterFace.nativeADLoad() {
            @Override
            public void loaded() {
                 Log.e("LBAD_Native1","load sucess" );
                showNativeAD1();
            }

            @Override
            public void loadedFail() {
                Log.e("LBAD_Native1","load fail" );

            }
        });
    }


    public void loadNewNativeAD2(){
        getNative2().load(LBAD_Config.getNativeAD2Width(MainActivity.this), LBAD_Config.getNativeAD2Height(MainActivity.this), new AdInterFace.nativeADLoad() {
            @Override
            public void loaded() {
                 Log.e("LBAD_Native2","load sucess" );
                showNativeAD2();
            }

            @Override
            public void loadedFail() {
                Log.e("LBAD_Native2","load fail" );

            }
        });
    }

    public void showNativeAD2(){
        getNative2().showAD( (ViewGroup) findViewById(R.id.ll_drawerLaout), LBAD_Config.getNativeAD2Width(MainActivity.this), LBAD_Config.getNativeAD2Height(MainActivity.this),  new AdInterFace.nativeADShow() {
            @Override
            public void show() {
                Log.e("LBAD_Native2","show " );

            }

            @Override
            public void userClose(String code, String msg, boolean isErr) {
                Log.e("LBAD_Native2","close " +msg +" isERR" +isErr );

            }

            @Override
            public void click() {
                Log.e("LBAD_Native2","click " );
            }
        });
    }

    public void showNativeAD1(){
         getNative1().showAD( (ViewGroup) findViewById(R.id.ll_drawerLaout), LBAD_Config.getNativeAD1Width(MainActivity.this), LBAD_Config.getNativeAD1Height(MainActivity.this), new AdInterFace.nativeADShow() {
             //信息流展示回调
             @Override
            public void show() {
                Log.e("LBAD_Native1","show " );
            }

             //信息流关闭回调
             @Override
            public void userClose(String code, String msg, boolean isErr) {
                Log.e("LBAD_Native1","userClose "+msg +" isErr"+isErr );

            }
             //信息流点击回调
            @Override
            public void click() {
                Log.e("LBAD_Native1","click " );

            }
        });
    }

    public void showRewwardVideo(){
        ADv.showVideo(LBAD_Config.REWARDVIDEO_ID, MainActivity.this, new AdInterFace.videoADCallBack() {
            //激励视频展示回调
            @Override
            public void show() {
                Log.e("LBAD_REWARDVIDEO","show");

            }
            //激励视频下发回调
            @Override
            public void showFullSucess() {
                Log.e("LBAD_REWARDVIDEO","showFullSucess");

            }
            //激励视频关闭回调
            @Override
            public void showFullFail(String code, String msg, boolean isErr) {
                Log.e("LBAD_REWARDVIDEO","showFullFail "+msg +" isErr "+isErr);

            }
            //激励视频播放完回调
            @Override
            public void videoComplete() {
                Log.e("LBAD_REWARDVIDEO","videoComplete ");

            }
        });
    }

    public void showInner(){

        ADv.showInner(LBAD_Config.INNER_ID,MainActivity.this, new AdInterFace.innerADCallBack() {
            //插屏点击回调
            @Override
            public void click() {
                android.util.Log.e("LBAD_INNER", "inner click");

            }
            //插屏错误和用户关闭
            @Override
            public void close(String code, String msg, boolean isErr) {
                android.util.Log.e("LBAD_INNER", "inner close "+code +" "+ msg +" isErr "+isErr);

            }
            //插屏展示回调
            @Override
            public void show() {
                android.util.Log.e("LBAD_INNER", "show  ");

            }

        });
    }

    public void showFullVideo(){
        ADv.showFullVideo(LBAD_Config.FULLVIDEO_ID, MainActivity.this, new AdInterFace.fullVideoCallBack() {
            //全屏视频展示回调
            @Override
            public void show() {
                android.util.Log.e("LBAD_FullVideo", "show");

            }
            //全屏视频播放完毕回调
            @Override
            public void showFullSucess() {
                android.util.Log.e("LBAD_FullVideo", "play compelte");

            }
            //全屏视频跳过回调
            @Override
            public void showFullFail(String code, String msg, boolean isErr) {
                android.util.Log.e("LBAD_FullVideo", "fail "+msg +" isErr "+isErr);

            }

        });
    }

    @Override
    protected void onResume() {
        //先于父方法调用
        if(nativeAD1!=null){
            nativeAD1.resume();
        }

        if(nativeAD2!=null){
            nativeAD2.resume();
        }
         super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //信息流销毁处理
        if(nativeAD1!=null){
            nativeAD1.destroy();
        }
        if(nativeAD2!=null){
            nativeAD2.destroy();
        }
        //bannner 销毁处理
        if(bannerAD!=null){
            bannerAD.destroy();
        }
     }

    @Override
    protected void onPause() {
        //先于父方法调用
        if(nativeAD1!=null){
            nativeAD1.pause();
        }
        if(nativeAD2!=null){
            nativeAD2.pause();
        }
        super.onPause();
    }

}
